package com.start.platform.phone;

import com.alibaba.fastjson2.JSONObject;
import com.start.platform.core.StartBasePlatformService;
import com.start.platform.core.entity.StartPlatformConfigEntity;
import com.start.platform.core.entity.StartPlatformFans;
import com.start.platform.core.service.StartPlatformFansService;
import com.start.platform.core.vo.StartPlatformFansDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class StartPhonePlatformService implements StartBasePlatformService<StartPlatformConfigEntity> {
    private final static Map<String, StartPlatformConfigEntity> platformServiceMap = new HashMap<>();
    @Autowired
    private StartPlatformFansService platformFansService;

    /**
     * 平台类型
     *
     * @return
     */
    @Override
    public String getPlatformType() {
        return "startUserPhone";
    }

    /**
     * 平台类型名称
     *
     * @return
     */
    @Override
    public String getPlatformTypeLabel() {
        return "手机号码";
    }

    /**
     * 添加配置
     *
     * @param config
     */
    @Override
    public void add(StartPlatformConfigEntity config) {
        if (platformServiceMap.containsKey(config.getAppId())) {
            return;
        }
        platformServiceMap.put(config.getAppId(), config);
    }

    /**
     * 移除配置
     *
     * @param appId
     */
    @Override
    public void remove(String appId) {
        if (!platformServiceMap.containsKey(appId)) {
            return;
        }
        platformServiceMap.remove(appId);
    }

    /**
     * 修改配置
     *
     * @param appId
     * @param config
     */
    @Override
    public void updateByAppId(String appId, StartPlatformConfigEntity config) {
        if (!platformServiceMap.containsKey(appId)) {
            return;
        }
        remove(config.getAppId());
        add(config);
    }

    /**
     * 是否存在AppId
     *
     * @param appId
     * @return
     */
    @Override
    public boolean hasAppId(String appId) {
        return platformServiceMap.containsKey(appId);
    }

    /**
     * 获取平台服务
     *
     * @param appId
     * @return
     */
    @Override
    public StartPlatformConfigEntity getPlatformService(String appId) {
        return platformServiceMap.get(appId);
    }


    /**
     * 根据第三方平台code 获取用户信息
     *
     * @param appId
     * @param code
     * @param prop
     * @return
     */
    @Override
    public StartPlatformFansDetail getUserInfoByPlatformCode(String appId, String code, JSONObject prop) {
        StartPlatformFans fans = platformFansService.getFansByOpenId(getPlatformType(), appId, code);
        StartPlatformFansDetail detail;
        JSONObject srcInfo = new JSONObject();
        srcInfo.put("openId", code);
        if (fans == null) {
            fans = new StartPlatformFans();
            fans.setPlatformOpenId(code);
            detail = StartPlatformFansDetail.create(fans);
            detail.setPlatformType(getPlatformType());
            detail.setPlatformAppId(appId);
            detail.setNickName(code);
            detail.setNewUser(true);
        } else {
            detail = StartPlatformFansDetail.create(fans);
        }
        detail.setPlatformFansInfo(srcInfo);
        platformFansService.saveOrUpdate(detail);
        return detail;
    }

    /**
     * 平台验证token
     *
     * @param appId
     * @return
     */
    @Override
    public Object platformVerifyToken(String appId) {
        return null;
    }
}
